//
//  TableScene.m
//  PokerPlus
//
//  Created by sunrui-MacBook on 11-8-4.
//  Copyright 2011 139.ME. All rights reserved.
//

#import "TableScene.h"



@implementation Table
//@synthesize delegate;
+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	Table *layer = [Table node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}
-(id) init
{
	if( (self=[super init] )) {
		self.isTouchEnabled = YES;
		numberOfPeople  = 9;
		[self addTable];
	}
	return self;
}

- (void)addTable {
	CGSize size = [[CCDirector sharedDirector] winSize];
	if (FBIsDeviceIPad()) {
		switch (numberOfPeople) {
			case 5:
				break;
				
			case 9:
				table = [CCSprite spriteWithFile:@"RoomTable_iPad.png"];
				table.scale = 0.8f;
				table.position = ccp(size.width/2,size.height -300);
				
				[table runAction:
				 [CCSpawn actions: 
				  [CCMoveTo actionWithDuration:0.2 position:ccp(size.width/2,size.height/2)],
				  [CCScaleTo actionWithDuration:0.2 scale:1]
				  ,nil]
				 ];
				
				[self addChild:table];
				
				[self performSelector:@selector(addSit) withObject:nil afterDelay:0.5];
				
				break;
			default:
				break;
		}
		
	}
	else {
		
	}
}

- (void)addSit{
	if (FBIsDeviceIPad()) {
		switch (numberOfPeople) {
			case 9:
			{
				CCMenuItemImage * mi[9];
				for (int i =0 ; i < 9 ; i++) {
					mi[i] = [CCMenuItemImage itemFromNormalImage:@"坐下.png" selectedImage:@"坐下.png" disabledImage:@"坐下.png" target:self selector:@selector(sitdown:)];
					_9tableMenu[i] = [CCMenu menuWithItems:mi[i],nil];
					if (i < 3) {
						_9tableMenu[i].position = ccp(300+170*i,240);
					}
					else if(i < 5){
						_9tableMenu[i].position = ccp(135,320 + 150 * (4-i));
					}
					else if (i < 7){
						_9tableMenu[i].position = ccp(805,320 + 150 * (6-i));
					}
					else if(i < 9){
						_9tableMenu[i].position = ccp(300+340*(8-i),560);
					}
					
					[self addChild:_9tableMenu[i]];
				}
				break;
			}
			default:
				break;
		}
	}
	else {
		
	}
	
}

//-(void) registerWithTouchDispatcher { 
//	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
//}
//
//-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
//	NSLog(@"Tablescene touch");
//	[delegate resignFirstResponder];
//	return YES;
//}
- (void)removeSit{
	if (FBIsDeviceIPad()) {
		switch (numberOfPeople) {
			case 9:
			{
				for (int i =0 ; i < 9; i++) {
					[_9tableMenu[i] removeFromParentAndCleanup:YES];
				}
				break;
			}
			default:
				break;
		}
	}
	else {
		
	}
	
}
- (void)sitdown:(CCMenuItemImage *)sender{
	//NSLog(@"%@",sender);
	
	[sender runAction:[CCOrbitCamera actionWithDuration:0.3f radius:0.8f deltaRadius:0.0f angleZ:0.0f deltaAngleZ:-180.0f angleX:0.0f deltaAngleX:0.0f]];
	//sender.visible = NO;
}

- (void)removeTable{
	[self removeSit];
	CGSize size = [[CCDirector sharedDirector] winSize];
	[table runAction:
	 [CCSpawn actions: 
	  [CCMoveTo actionWithDuration:0.25 position:ccp(size.width/2,size.height-300)],
	  [CCScaleTo actionWithDuration:0.25 scale:0.8f]
	  ,nil]
	 ];
}

- (void)dealloc{

	[self removeFromParentAndCleanup:YES];
	[super dealloc];
}

@end
